<%@ include file="/common/taglibs.jsp"%>
<%@ page import="idv.takeshi.software.productline.bookstore.domain.model.order.Order"%>
<%@ page import="idv.takeshi.software.productline.bookstore.domain.model.order.Payment"%>
<%@ page import="idv.takeshi.software.productline.bookstore.domain.model.order.CreditCard"%>
<%@page import="idv.takeshi.software.productline.bookstore.domain.model.order.BeforeDeliveryPayment"%>
<%@page import="idv.takeshi.software.productline.bookstore.domain.model.order.Atm"%>
<%@page import="idv.takeshi.software.productline.bookstore.domain.model.order.PostalOrder"%>
<%@page import="idv.takeshi.software.productline.bookstore.domain.model.order.CashInConvenientStore"%>

<c:set var="goToFillUpOrderInfoUrl"><spring:url value="/order/orderDetailsInfo.html"/></c:set>
<c:set var="goToPersistOrderUrl"><spring:url value="/order/persistOrder.html"/></c:set>

<c:set var="goToPayByCreditCardUrl"><spring:url value="/payment/payByCreditCard.html">
	<spring:param name="orderId" value="${order.id}"/>
	<spring:param name="previousPath" value="${param.previousPath}"/>
	<spring:param name="seeOrderDetailsInfo" value="${param.seeOrderDetailsInfo}"/>
</spring:url></c:set>

<c:set var="previousPathUrl"><spring:url value="${param.previousPath}"/></c:set>

<head>
	<title><spring:message code="orders.maintainOrders.header" arguments="${order.id}"/></title>
	<link rel="stylesheet" type="text/css" href="<spring:url value='/styles/shoppingCart.css'/>"/>
	
	<script type="text/javascript">
	$(function(){
		$("#goBackToPreviousPath").click(function(){
			location.assign("${previousPathUrl}");
		});
	});
	</script>
</head>
<p>
	<h1><spring:message code="orders.maintainOrders.header" arguments="${order.id}"/></h1>
</p>
<table width="100%">
	<tr>
		<td>
		<h1><spring:message code="orders.maintainOrders.order.orderItems.header"/></h1>
		<table width="100%" class="shoppingCart">
			<tr>
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.book.name"/></th>
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.book.unitPrice"/></th>
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.quantity"/></th>
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.total"/></th>
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.status"/></th>
				
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.book.inventory.status"/></th>
				<th><spring:message code="orders.maintainOrders.order.orderItems.orderItem.book.inventory.quantity"/></th>
			</tr>
			<c:forEach var="item" items="${order.orderItems}">
				<tr>
					<td><a href="<spring:url value='#'/>"><c:out value="${item.orderedBook.name}"/></a></td>
					<td><c:out value="${item.orderedBook.unitPrice}"/></td>
					<td><c:out value="${item.quantity}"/></td>
					<td><c:out value="${item.totalUnitPrice}"/></td>
					<td><spring:message code="${sf:getOrderItemStatusMessageCode(item.status)}"/></td>
					<td><spring:message code="${sf:getInventoryStatusMessageCode(item.orderedBook.inventory.status)}"/></td>
					<td><c:out value="${item.orderedBook.inventory.availableQuantity}"/></td>
				</tr>
			</c:forEach>
		</table>
		
		<table>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.deliveryService.fee"/></th>
				<td>$<c:out value="${order.deliveryFee}"></c:out></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.price"/></th>
				<td>$<c:out value="${order.price}"></c:out></td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td>
		<p></p>
		<h1><spring:message code="orders.maintainOrders.order.header"/></h1>
		<c:if test="${!empty param.errorMsg}">
	        <div class="ui-widget">
				<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
					<div><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
					${param.errorMsg}</div>
				</div>
			</div>
	    </c:if>
		<table class="shoppingCart" width="100%">
			<tr>
				<th><spring:message code="orders.maintainOrders.order.userAccount.accountName"/></th>
				<td><c:out value="${userAccount.accountName}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.userAccount.userName"/></th>
				<td><c:out value="${userAccount.userName}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.userAccount.cellPhoneNumber"/></th>
				<td><c:out value="${userAccount.cellPhoneNumber}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.recipient.name"/></th>
				<td><c:out value="${order.recipientName}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.recipient.cellPhoneNumber"/></th>
				<td><c:out value="${order.recipientCellPhoneNumber}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.recipient.address"/></th>
				<td><c:out value="${order.recipientAddress}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.payment"/></th>
				<td><spring:message code="${sf:getPaymentMessageCode(order.payment)}"/> 
				<%
				Order order = (Order)request.getAttribute("order");
				Payment payment = order.getPayment();
				if(!(payment instanceof CreditCard)){
				%>
					<%
					if(payment instanceof Atm){
					%>
					| <a href="<spring:url value="/orders/maintainAtmPayment.html">
						<spring:param name="orderId" value="${order.id}"/>
						<spring:param name="previousPath" value="${currentPath}"/>
					</spring:url>"><spring:message code="orders.maintainOrders.order.payment.maintain"/></a>
					<%
					} else if(payment instanceof PostalOrder){
					%>
					| <a href="<spring:url value="/orders/maintainPostalOrderPayment.html">
						<spring:param name="orderId" value="${order.id}"/>
						<spring:param name="previousPath" value="${currentPath}"/>
					</spring:url>"><spring:message code="orders.maintainOrders.order.payment.maintain"/></a>
					<%
					} else if(payment instanceof CashInConvenientStore){
					%>
					| <a href="<spring:url value="/orders/maintainCashInConvinientStorePayment.html">
						<spring:param name="orderId" value="${order.id}"/>
						<spring:param name="previousPath" value="${currentPath}"/>
					</spring:url>"><spring:message code="orders.maintainOrders.order.payment.maintain"/></a>
					<%
					}
					%>
				<%	
				}
				%>
				</td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.payment.status"/></th>
				<td><c:choose>
					<c:when test="${order.payment.paid}">
						<spring:message code="orders.maintainOrders.order.payment.status.yes"/>
					</c:when>
					<c:otherwise>
						<spring:message code="orders.maintainOrders.order.payment.status.no"/>
					</c:otherwise>
				</c:choose>
				</td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.deliveryService.type"/></th>
				<td><c:out value="${order.deliveryService.name}"/></td>
			</tr>
			
			<tr>
				<th><spring:message code="orders.maintainOrders.order.invoice.type"/></th>
				<td><spring:message code="${sf:getInvoiceMessageCode(order.invoice)}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.invoice.invoiceNumber"/></th>
				<td><spring:message code="${order.invoice.invoiceNumber}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.invoice.title"/></th>
				<td><c:out value="${order.invoice.invoiceTitle}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.invoice.uniformNumber"/></th>
				<td><c:out value="${order.invoice.uniformNumber}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.extraRequirements"/></th>
				<td><c:out value="${order.extraRequirements}"/></td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.status"/></th>
				<td><spring:message code="${sf:getOrderStatusMessageCode(order.status)}"/> 
				<%
				switch(order.getStatus()){
				case NEW:
				case WAIT_FOR_REPLENISHMENT:
				%>
				| <a href='<spring:url value='/orders/nextStatus.html'>
					<spring:param name="orderId" value="${order.id}"/>
					<spring:param name="methodName" value="process"/>
					<spring:param name="previousPath" value="${currentPath}"/>
				</spring:url>'><spring:message code="orders.maintainOrders.order.status.next"/></a>
				<%
					break;
				case UNDER_PROCESSING:
				%>
				| <a href='<spring:url value='/orders/nextStatus.html'>
					<spring:param name="orderId" value="${order.id}"/>
					<spring:param name="methodName" value="deliver"/>
					<spring:param name="previousPath" value="${currentPath}"/>
				</spring:url>'><spring:message code="orders.maintainOrders.order.status.next"/></a>
				<%
					break;
				case UNDER_DELIVERY:
				%>
				| <a href='<spring:url value='/orders/nextStatus.html'>
					<spring:param name="orderId" value="${order.id}"/>
					<spring:param name="methodName" value="arrive"/>
					<spring:param name="previousPath" value="${currentPath}"/>
				</spring:url>'><spring:message code="orders.maintainOrders.order.status.next"/></a>
				<%
					break;
				case IN_CONVENIENT_STORE:
				case RECEIVED:
				%>
				| <a href='<spring:url value='/orders/nextStatus.html'>
					<spring:param name="orderId" value="${order.id}"/>
					<spring:param name="methodName" value="close"/>
					<spring:param name="previousPath" value="${currentPath}"/>
				</spring:url>'><spring:message code="orders.maintainOrders.order.status.next"/></a>
				<%
					break;
				case CLOSE:
				case CANCEL:
				//DO NOTHING
					break;
				default:
				%>
				| <div class="ui-widget">
					<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
						<div><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
						<spring:message code="orders.maintainOrders.order.status.error" arguments="${order.status}" /></div>
					</div>
				</div>
				<%
				}
				%>
				
				<%
				switch(order.getStatus()){
				case NEW:
				case WAIT_FOR_REPLENISHMENT:
				case UNDER_PROCESSING:
				%>
				| <a href="<spring:url value='/orders/cancelOrder.html'>
					<spring:param name="orderId" value="${order.id}"/>
					<spring:param name="previousPath" value="${currentPath}"/>
				</spring:url>"><spring:message code="orders.maintainOrders.order.status.cancel"/></a>
				<%
					break;
				}
				%>
				</td>
			</tr>
			<tr>
				<th><spring:message code="orders.maintainOrders.order.pickingList"/></th>
				<td>
				<c:set var="numberOfPickingLists" value="${fn:length(order.pickingLists)}"/>
				<c:choose>
					<c:when test="${numberOfPickingLists > 0}">
						<a href="<spring:url value='/pickingLists/listPickingLists.html'>
							<spring:param name='orderId' value='${order.id}'/>
							<spring:param name='previousPath' value='${currentPath}'/>
						</spring:url>"><spring:message code="orders.maintainOrders.order.pickingList.yes" arguments="${numberOfPickingLists}"/></a>
					</c:when>
					<c:otherwise>
						<spring:message code="orders.maintainOrders.order.pickingList.no"/>
					</c:otherwise>
				</c:choose>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<button id="goBackToPreviousPath" class="aButton" style="float: right; margin-right: 10px;"><spring:message code="orders.maintainOrders.goBackToPreviousPath"/></button>
		</td>
	</tr>
</table>
